package com.mo.axion.handler.deduplication.service;

import cn.hutool.core.text.StrPool;
import com.mo.axion.common.domain.TaskInfo;
import com.mo.axion.common.enums.DeduplicationType;
import com.mo.axion.handler.deduplication.limit.LimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author Mo
 * @createTime 2024/2/20 22:17
 * @description 频次去重服务
 */
@Service
public class FrequencyDeduplicationService extends AbstractDeduplicationService{

    private static final String PREFIX = "FRE";

    @Autowired
    public FrequencyDeduplicationService(@Qualifier("SimpleLimitService")LimitService limitService) {
        this.limitService = limitService;
        deduplicationType = DeduplicationType.FREQUENCY.getCode();
    }

    /**
     * 构建去重key
     *  key ： receiver + templateId + sendChannel
     *  一天内一个用户只能收到某个渠道的消息 N 次
     * @param taskInfo
     * @param receiver
     * @return
     */
    @Override
    public String deduplicationSingleKey(TaskInfo taskInfo, String receiver) {
        return PREFIX + StrPool.C_UNDERLINE
                + receiver + StrPool.C_UNDERLINE
                +taskInfo.getMessageTemplateId() + StrPool.C_UNDERLINE
                +taskInfo.getSendChannel();
    }
}
